package com.ray.auth.shiro;

import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.rpc.RpcException;
import com.ray.auth.system.emun.ManagerType;
import com.ray.auth.system.service.ButtonService;
import com.ray.auth.system.service.MenuService;
import com.ray.auth.system.service.UserService;
import com.ray.auth.system.table.entity.Button;
import com.ray.auth.system.table.entity.Menu;
import com.ray.auth.system.table.entity.User;
import com.xiaoleilu.hutool.util.ObjectUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import com.zjhc.flyhong.common.function.error.MessageHandle;
import com.zjhc.shiro.db.RealmDataBaseRead;
import com.zjhc.shiro.entity.ShiroUser;

/**
 * <h1>自定义读取数据库信息 </h1>
 * @创建人 ray
 * @创建时间 2017-06-12
 * @版本 1.0
 * @重要修改备注 
 * 
 */
public class HongWebRealm implements RealmDataBaseRead{
	private static final Logger LOGGER = Logger.getLogger(HongWebRealm.class);

	@Autowired
    private UserService userService;
	@Autowired
    private MenuService menuService;
	@Autowired
    private ButtonService buttonService;
	
	
	
	public ShiroUser queryUser(String userName) {
		ShiroUser shiroUser = null;
		try {
			if(StrUtil.isNotBlank(userName)){
				//获取用户信息
				User user = userService.queryUser(userName);
				if(ObjectUtil.isNotNull(user)){
					shiroUser = new ShiroUser();
					shiroUser.setUserName(userName);
					shiroUser.setPassword(user.getPassword());
					shiroUser.setExpendValue(user);
				}
			}
		} catch (RpcException e) {//Rpc异常
			LOGGER.error("RPC调用异常");
		}catch (Exception e) {
			LOGGER.error("内部异常");
		}
		return shiroUser;
	}

	
	public Set<String> queryResours(String userName) {
		//放置请求路径 及页面路径
		Set<String> set = null;
		try {
			if(StrUtil.isNotBlank(userName)){
				//获取用户信息
				User user = userService.queryUser(userName);
				if(ObjectUtil.isNotNull(user)){
					//应用id
					String appid = MessageHandle.getMsgByKey("app.id");
					set = new HashSet<String>();
					//超级管理员默认拥有所有权限
					if(StrUtil.equals(user.getManagerType(), ManagerType.SUPER.getType())){
						//菜单路径
						for(Menu menu : menuService.querySuperUserMenus(appid)){
							//放置用户请求路径
							set.add(menu.getAuthUrl());
						}
						//按钮路径
						for(Button button : buttonService.querySuperUserButtons(appid)){
							//放置用户请求路径
							set.add(button.getAuthUrl());
						}
					}else{
						//菜单路径
						for(Menu menu : menuService.queryUserMenus(user.getUserId(),appid)){
							//放置用户请求路径
							set.add(menu.getAuthUrl());
						}
						//按钮路径
						for(Button button : buttonService.queryUserButtons(user.getUserId(),appid)){
							//放置用户请求路径
							set.add(button.getAuthUrl());
						}
					}
					
				}
			}
		} catch (RpcException e) {//Rpc异常
			LOGGER.error("RPC调用异常");
		}catch (Exception e) {
			LOGGER.error("内部异常");
		}
		return set;
	}

}
